package com.ms.game.billing;

import java.math.BigDecimal;
import java.util.Calendar;

import org.hibernate.Query;
import org.hibernate.Session;

import com.ms.game.db.util.UpdateQueryBuilder;
import com.xpec.c4.db.basic.BasicGameDAO;
import com.xpec.c4.db.hibernate.manager.HibernateManager;
import com.xpec.c4.game.common.Helper;
import com.xpec.c4.game.time.TimeSys;

public class GameBillingDAO extends BasicGameDAO {
	
	public static boolean update(GameBillingModel model){		
		Session session=HibernateManager.txBegin();
		session.update(model);
		return HibernateManager.txCommitFailRollbackClearSession();
	}
	
	
	/**
	 * 讀取訂單資料
	 * @return
	 */
	public static GameBillingModel getBillingByOrderID(String orderID, String playerID)	{
		Session session = HibernateManager.getSession();
		Query query = session.createQuery("from GameBillingModel where playerID = :PLAYERID and orderID=:ORDERID");
		query.setParameter("PLAYERID", playerID);
		query.setParameter("ORDERID", orderID);
		GameBillingModel model = (GameBillingModel) query.uniqueResult();
		HibernateManager.endSession();
		return model;
	}
	
	/**
	 * 用dbId 取得 訂單 
	 */
	public static GameBillingModel getGameBillingModelbyDBId(String dbId){
		Session session = HibernateManager.getSession();
		Query query = session.createQuery("from GameBillingModel where db_id = :DBID");
		query.setParameter("DBID", dbId);
		GameBillingModel model = (GameBillingModel) query.uniqueResult();
		HibernateManager.endSession();
		return model;
	}
	
	/**
	 * 新增資料
	 * @param model
	 * @return
	 */
	public static boolean addOrder(GameBillingModel model)	{
		Calendar rightNow = Calendar.getInstance();
		
		model.setVerify_stat(GameBillingModel.STAT_CREATE);
		model.setEventTime(rightNow.getTime());
		model.setCreate_time(TimeSys.currentTimeSec());
		model.setModify_time(model.getCreate_time());
		
		Session session=HibernateManager.txBegin();
		session.save(model);
		return HibernateManager.txCommitFailRollbackClearSession();
	}
	
	
	/**
	 * 更新訂單狀態
	 * @param orderid
	 * @param stat
	 * @return
	 */
	public static boolean modifyVerifyStat(String orderID, String playerID, String stat)	{
		 Session session=HibernateManager.txBegin();
		 UpdateQueryBuilder builder = new UpdateQueryBuilder();
		 builder.setSession(session);
		 builder.setTableName("GameBillingModel");
		 builder.addSetValue("verify_stat", stat);
		 builder.addSetValue("modify_time", TimeSys.currentTimeSec());
		 builder.addWhereKey("orderID", orderID);
		 builder.addWhereKey("playerID", playerID);
		 builder.executeQuery();
		 return HibernateManager.txCommitFailRollbackClearSession();
	}
	
	/**
	 * 把PurchaseInfo Signature IapOrderId  寫進去
	 */
	public static boolean updateGoogleResults(String purchaseInfo, String signature, String iapOrderId, String orderID, String playerID) {
		Session session = HibernateManager.txBegin();
		UpdateQueryBuilder builder = new UpdateQueryBuilder();
		builder.setSession(session);
		builder.setTableName("GameBillingModel");
		builder.addSetValue("purchaseInfo", purchaseInfo);
		builder.addSetValue("signature", signature);
		builder.addSetValue("iapOrderId", iapOrderId);		
		builder.addWhereKey("orderID", orderID);
		builder.addWhereKey("playerID", playerID);
		builder.executeQuery();
		return HibernateManager.txCommitFailRollbackClearSession();
	}
	
	public static boolean updateAppleReceipt(String receipt, String orderID ,String playerID){
		Session session = HibernateManager.txBegin();
		UpdateQueryBuilder builder = new UpdateQueryBuilder();
		builder.setSession(session);
		builder.setTableName("GameBillingModel");
		builder.addSetValue("receipt", receipt);
		builder.addWhereKey("orderID", orderID);
		builder.addWhereKey("playerID", playerID);
		builder.executeQuery();
		return HibernateManager.txCommitFailRollbackClearSession();
	}
	
	public static boolean updateApplePurchaseInfo(String orderID ,String playerID,String iapOrderID,String purchaseInfo){
		Session session = HibernateManager.txBegin();
		UpdateQueryBuilder builder = new UpdateQueryBuilder();
		builder.setSession(session);
		builder.setTableName("GameBillingModel");
		builder.addSetValue("iapOrderID", iapOrderID);
		builder.addSetValue("purchaseInfo", purchaseInfo);
		builder.addWhereKey("orderID", orderID);
		builder.addWhereKey("playerID", playerID);
		builder.executeQuery();
		return HibernateManager.txCommitFailRollbackClearSession();
	}
	
	public static boolean updatePlatFromParams(String params,String orderID, String playerID){
		Session session = HibernateManager.txBegin();
		UpdateQueryBuilder builder = new UpdateQueryBuilder();
		builder.setSession(session);
		builder.setTableName("GameBillingModel");
		builder.addSetValue("params", params);
		builder.addWhereKey("orderID", orderID);
		builder.addWhereKey("playerID", playerID);
		builder.executeQuery();
		return HibernateManager.txCommitFailRollbackClearSession();
	}
	
	
	/**
	 * 寫入平台給的json
	 */
	public static boolean updatePlatformResponse(String platformResult,String orderID , String playerID){
		 Session session=HibernateManager.txBegin();
		 Query query = session.createQuery("Update GameBillingModel set platformresult = :RESULT  Where orderID=:ORDERID and playerID=:PLAYERID");
		 query.setParameter("RESULT", platformResult);
		 query.setParameter("ORDERID", orderID);
		 query.setParameter("PLAYERID", playerID);
		 query.executeUpdate();
		 return HibernateManager.txCommitFailRollbackClearSession();
	}
	
	public static StoreLimitModel checkMonthlyStoreLimit(String accountId, String pid) {
		Calendar calendar = Calendar.getInstance();
		int month = calendar.get(Calendar.YEAR) * 100 + calendar.get(Calendar.MONTH) + 1;
		
		Session session = HibernateManager.getSession();
		Query query = session.createQuery("From StoreLimitModel Where accountId = :ACCOUNTID and pid = :PID and month = :MONTH");
		query.setParameter("ACCOUNTID", accountId);
		query.setParameter("PID", pid);
		query.setParameter("MONTH", month);
		StoreLimitModel model = (StoreLimitModel)query.uniqueResult();
		HibernateManager.endSession();
		return model;
	}
	
	public static boolean insertOrUpdateStoreMoney(String accountId, String pid, double addMoney){
		Calendar calendar = Calendar.getInstance();
		int yearMonth = calendar.get(Calendar.YEAR) * 100 + calendar.get(Calendar.MONTH) + 1;
		
		Session session = HibernateManager.txBegin();
		Query query = session.createQuery("From StoreLimitModel Where accountId = :ACCOUNTID and pid = :PID and month = :MONTH");
		query.setParameter("ACCOUNTID", accountId);
		query.setParameter("PID", pid);
		query.setParameter("MONTH", yearMonth);
		StoreLimitModel model = (StoreLimitModel)query.uniqueResult();
		BigDecimal add = new BigDecimal(String.valueOf(addMoney)); 
		if(model == null){
			model = new StoreLimitModel();
			model.setAccountId(accountId);
			model.setPid(pid);
			model.setMonth(yearMonth);
			model.setTotalStoreMoney(add);
		}else{
			BigDecimal sum =  Helper.add(model.getTotalStoreMoney(), add);
			model.setTotalStoreMoney(sum);
		}
		session.saveOrUpdate(model);
		return HibernateManager.txCommitFailRollbackClearSession();
	}
	
	/**
	 * 讀取限制名單model
	 * @param accountId
	 * @return
	 */
	public static GameBillingRestrictedListModel loadGameBillingRestrictedListModel(String accountId) {
		Session session = HibernateManager.getSession();
		Query query = session.createQuery("From GameBillingRestrictedListModel Where accountId = :ACID");
		query.setParameter("ACID", accountId);
		GameBillingRestrictedListModel model = (GameBillingRestrictedListModel) query.uniqueResult();
		HibernateManager.endSession();
		return model;
	}
	
	/**
	 * 新增 GameBillingRestrictedListModel 
	 * @param accountId
	 * @param appStoreEndTime
	 * @param appStoreLimit
	 * @param googlePlayEndTime
	 * @param googlePlayLimit
	 * @return
	 */
	public static boolean insertOrUpdateRestrictedListModel(String accountId, long appStoreEndTime, double appStoreLimit, long googlePlayEndTime, double googlePlayLimit) {
		GameBillingRestrictedListModel model = new GameBillingRestrictedListModel();
		model.setAccountId(accountId);
		model.setAppStoreEndTime(appStoreEndTime);
		model.setAppStoreLimit(new BigDecimal(String.valueOf(appStoreLimit)));
		model.setGooglePlayEndTime(googlePlayEndTime);
		model.setGooglePlayLimit((new BigDecimal(String.valueOf(googlePlayLimit))));
		
		Session session = HibernateManager.txBegin();
		session.saveOrUpdate(model);
		return HibernateManager.txCommitFailRollbackClearSession();
	}
	
	/**
	 * 刪除 GameBillingRestrictedListModel
	 * @param accountId
	 * @return
	 */
	public static boolean deleteRestrictedListModel(String accountId){
		Session session = HibernateManager.txBegin();
		Query query = session.createQuery("Delete From GameBillingRestrictedListModel Where accountId = :ACID");
		query.setParameter("ACID", accountId);
		query.executeUpdate();
		return HibernateManager.txCommitFailRollbackClearSession();
	}
}
